import maya.cmds as mc

import datetime


class exportData_realTime():

    def __init__(self):
        pass
       
    def UI(self):    
    
        exportData_UI = 'exportData_UI'
        if mc.window(exportData_UI,ex = True):
            mc.deleteUI(exportData_UI)

        mc.window(exportData_UI)
        
        mc.columnLayout()
        
        mc.intField( 'frame_date' , value = 60 , editable=True)
        mc.intField( 'IfPause_intF' , value=0,editable=False)
        
        mc.button( 'start_bn' , l = 'start' , w= 150 , c = lambda *args:self.start(  ) )
        mc.button( 'execute' , l = 'start' , w= 150 , c = lambda *args:self.changeButton_vis(  ) )
        
        mc.showWindow(exportData_UI)

    
    def changeButton_vis(self):
    
        current = mc.button( 'execute' , q=True, l = True )
        if current == 'start':
            mc.button( 'execute' , e=True, l = 'cancle' )
            mc.intField( 'IfPause_intF' ,e=True, value = 1 )
        if current == 'cancle':
            mc.button( 'execute' , e=True, l = 'start' )
            mc.intField( 'IfPause_intF' ,e=True, value = 0 )
    
    
    def start(self):
    
        self.exportData()
    
    def exportData(self):
        
        FrameRate = mc.intField( 'frame_date' , q=True , value = True)
        
        
        cishu = 0
        starttime = datetime.datetime.now()
        
        ver = 1 
        while ver == 1 :
            
            pause = mc.intField( 'IfPause_intF' ,q=True, value = True )
            if pause == 0 :
                diff = 1.0/FrameRate
                currenttime = datetime.datetime.now()
                
                diff_value = currenttime - starttime
                
                diff_ms    = str(diff_value).split(':')[-1]
                #print(diff_ms)
                
                
                diff_frame = float(diff_ms)
                
                if diff_frame >= diff:
                    print(diff_frame,diff)
                    starttime = currenttime
                    print starttime
                    
                    cishu = cishu+1
                    
                else:
                    
                    #cishu = cishu+1
                    continue
                print (cishu)
                
                cishu = cishu+1
                if cishu >= 1000:
                    break
            else:
            
                break 